package com.fire.eyes.controller;

import com.fire.core.util.AjaxResult;
import com.fire.eyes.service.ISiteService;
import com.fire.eyes.service.device.DeviceService;
import com.fire.eyes.validation.*;
import com.smartthing.spi.SmartThingException;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author yangxiaotian
 * @version 1.0
 * @date 2019/1/17 11:21
 * @description:
 */
@Controller
public class DeviceController {
    private final static Logger blogin = Logger.getLogger(DeviceController.class);
    @Autowired
    private DeviceService deviceService;
    @Autowired
    private ISiteService iSiteService;

    /**
     * 查询设备（传感器）列表
     *
     * @return
     * @author yangxiaotian
     */
    @PostMapping(value = "/getdevices")
    @ResponseBody
    public AjaxResult getdevices(@RequestBody @Validated SiteIdDTO siteIdDTO, BindingResult bindingResult) throws SmartThingException {
        AjaxResult result = null;
        if (bindingResult.hasErrors()) {
            for (FieldError fieldError : bindingResult.getFieldErrors()) {
                System.out.println(fieldError.getDefaultMessage());
            }
        }
        result = deviceService.queryDevicesList(siteIdDTO);
        return result;
    }


    /**
     * 查询设备（传感器）详情
     *
     * @return
     * @author yangxiaotian
     */
    @PostMapping(value = "/getdevicedetail")
    @ResponseBody
    public AjaxResult getdevicedetail(@RequestBody @Validated DeviceIdDTO deviceIdDTO, BindingResult bindingResult) throws SmartThingException {
        if (bindingResult.hasErrors()) {
            for (FieldError fieldError : bindingResult.getFieldErrors()) {
                System.out.println(fieldError.getDefaultMessage());
            }
        }

        AjaxResult result = deviceService.queryDeviceByid(deviceIdDTO);
        return result;
    }

    /**
     * 查询所有设备（传感器）品类
     *
     * @param json 传入参数
     * @return
     * @author yangxiaotian
     */
    @PostMapping(value = "/accetcategory")
    @ResponseBody
    public AjaxResult accetcategory(@RequestBody @Validated TokenDTO tokenDTO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            for (FieldError fieldError : bindingResult.getFieldErrors()) {
                System.out.println(fieldError.getDefaultMessage());
            }
        }
        AjaxResult result = deviceService.queryDeviceCategory(tokenDTO);
        return result;
    }

    /**
     * 增加设备（传感器）接口
     *
     * @param json 传入参数
     * @return
     * @author raoyuxing
     */
    @PostMapping(value = "/adddevice")
    @ResponseBody
    public AjaxResult adddevice(@RequestBody @Validated AddDeviceReqDTO addDeviceReqDTO, BindingResult bindingResult) throws SmartThingException {
        if (bindingResult.hasErrors()) {
            for (FieldError fieldError : bindingResult.getFieldErrors()) {
                System.out.println(fieldError.getDefaultMessage());
            }
        }
        AjaxResult result = deviceService.creatDevices(addDeviceReqDTO);

        return result;
    }

    /**
     * 创建设备关联
     *
     * @param json 传入参数
     * @return
     * @author yangxiaotian
     */
    @PostMapping(value = "/assign")
    @ResponseBody
    public AjaxResult diveAssignments(@RequestBody @Validated CreatDeviceAssignmentsDTO creatDeviceAssignmentsDTO, BindingResult bindingResult) throws SmartThingException {
        if (bindingResult.hasErrors()) {
            for (FieldError fieldError : bindingResult.getFieldErrors()) {
                System.out.println(fieldError.getDefaultMessage());
                return new AjaxResult("0",fieldError.getDefaultMessage(),fieldError.getDefaultMessage());
            }
        }
        AjaxResult result = deviceService.CreatDeviceAssignments(creatDeviceAssignmentsDTO);

        return result;
    }

}
